package com.example.demo.nuoqi.service.impl;

import com.example.demo.nuoqi.dao.DirectionMapper;
import com.example.demo.nuoqi.pojo.Direction;
import com.example.demo.nuoqi.service.DirectionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:Direction业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class DirectionServiceImpl implements DirectionService {

    @Autowired
    private DirectionMapper directionMapper;


    /**
     * Direction条件+分页查询
     * @param direction 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Direction> findPage(Direction direction, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(direction);
        //执行搜索
        return new PageInfo<Direction>(directionMapper.selectByExample(example));
    }

    /**
     * Direction分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Direction> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Direction>(directionMapper.selectAll());
    }

    /**
     * Direction条件查询
     * @param direction
     * @return
     */
    @Override
    public List<Direction> findList(Direction direction){
        //构建查询条件
        Example example = createExample(direction);
        //根据构建的条件查询数据
        return directionMapper.selectByExample(example);
    }


    /**
     * Direction构建查询对象
     * @param direction
     * @return
     */
    public Example createExample(Direction direction){
        Example example=new Example(Direction.class);
        Example.Criteria criteria = example.createCriteria();
        if(direction!=null){
            // 
            if(!StringUtils.isEmpty(direction.getId())){
                    criteria.andEqualTo("id",direction.getId());
            }
            // 
            if(!StringUtils.isEmpty(direction.getDirectionName())){
                    criteria.andEqualTo("directionName",direction.getDirectionName());
            }
            // 
            if(!StringUtils.isEmpty(direction.getProfessionName())){
                    criteria.andEqualTo("professionName",direction.getProfessionName());
            }
            // 
            if(!StringUtils.isEmpty(direction.getProfessionId())){
                    criteria.andEqualTo("professionId",direction.getProfessionId());
            }
            // 
            if(!StringUtils.isEmpty(direction.getStatus())){
                    criteria.andEqualTo("status",direction.getStatus());
            }
            // 
            if(!StringUtils.isEmpty(direction.getCreateTime())){
                    criteria.andEqualTo("createTime",direction.getCreateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        directionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Direction
     * @param direction
     */
    @Override
    public void update(Direction direction){
        directionMapper.updateByPrimaryKey(direction);
    }

    /**
     * 增加Direction
     * @param direction
     */
    @Override
    public void add(Direction direction){
        directionMapper.insert(direction);
    }

    /**
     * 根据ID查询Direction
     * @param id
     * @return
     */
    @Override
    public Direction findById(Integer id){
        return  directionMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Direction全部数据
     * @return
     */
    @Override
    public List<Direction> findAll() {
        return directionMapper.selectAll();
    }
}
